﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Web;

using Credinka.Ayni.SEA.EN;
using Credinka.Ayni.SEA.RN;

public class Util : System.Web.UI.Page
{
    public const string correo_sistema = "ayni@credinka.com";
    public const string dominio_sistema = "@credinka.com";

    public string ConsultarCorreosPorObjetoyFormulario(int IdFormulario, int CodigoObjeto)
    {
        try
        {           
            rnFormularioObjeto ornFormularioObjeto = new rnFormularioObjeto();
            enFormularioObjeto oenFormularioObjeto = new enFormularioObjeto();
            oenFormularioObjeto.IdFormulario = IdFormulario;
            oenFormularioObjeto.CodigoObjeto = CodigoObjeto;

            List<enAlerta> loenAlerta = new List<enAlerta>();
            loenAlerta = ornFormularioObjeto.fConsultarCorreosPorObjetoyFormulario(oenFormularioObjeto);

            string To = string.Empty;
            string Subject = string.Empty;
            string Body = string.Empty;

            for (int i = 0; i < loenAlerta.Count; i++)
            {
                To += loenAlerta[i].Usuario + dominio_sistema + ",";
            }

            if (loenAlerta.Count > 0)
            {
                Subject = loenAlerta[0].Asunto;
                Body = loenAlerta[0].Mensaje;
                To = To.Substring(0, To.Length - 1);

                this.EnviarCorreo(correo_sistema, "cesarvallejo09@gmail.com,lesly.varillas@gmail.com", Subject, Body);    
                //this.EnviarCorreo(correo_sistema, To, Subject, Body);    
            }

            return "Ok";
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    public string EnviarCorreo(string From, string To, string Subject, string Body)
    {
        //Envío de correo - Inicio
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.To.Add(To);
        msg.Bcc.Add("lormeno@credinka.com");
        msg.From = new MailAddress(From, "CREDINKA", System.Text.Encoding.UTF8);
        msg.Subject = Subject;
        msg.SubjectEncoding = System.Text.Encoding.UTF8;
        Body += "<img src='cid:logo-ayni' width=276 height=94 style='text-align:right; float:right; position:relative' title='Core Bancario'/>";
        msg.Body = Body;
        msg.BodyEncoding = System.Text.Encoding.UTF8;
        msg.IsBodyHtml = true;
        msg.Priority = MailPriority.Normal;

        AlternateView alternateView = AlternateView.CreateAlternateViewFromString(Body, Encoding.UTF8, MediaTypeNames.Text.Html);

        LinkedResource lnkResCredinka = new LinkedResource(Server.MapPath("~\\images\\logo-ayni-interno.png"), MediaTypeNames.Image.Jpeg);
        lnkResCredinka.ContentId = "logo-ayni";

        alternateView.LinkedResources.Add(lnkResCredinka);
        msg.AlternateViews.Add(alternateView);

        SmtpClient client = new SmtpClient();

        // Prueba con Gmail - Inicio
        client.Credentials = new System.Net.NetworkCredential("lesly.varillas@gmail.com", "123qwe");
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        // Prueba con Gmail - Fin

        //client.Credentials = new System.Net.NetworkCredential("ayni@credinka.com", "Credinka2013");
        //client.Port = 25;
        //client.Host = "mail.credinka.com";    

        try
        {
            client.Send(msg);
            return "Ok";
        }
        catch (System.Net.Mail.SmtpException ex)
        {
            return ex.Message;
        }
        //Envío de correo - Fin
    }    
}